package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.pojo.SetmealDish;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 自定义分页查询
     */
    @Override
    public Page<SetmealDto> selectByPage(Integer page, Integer pageSize, String name) {
        //创建分页对象,设置分页参数
        Page<Dish> p = new Page<>(page, pageSize);
        //调用mapper层自定义分页查询方法并返回
        return setmealMapper.selectByPage(p, name);
    }

    /**
     * 自定义添加套餐方法
     * @param setmealDto
     */
    @Override
    public void add(SetmealDto setmealDto) {
        //调用方法往setmeal表添加
        setmealMapper.insert(setmealDto);
        //获取setmealDto中的SetmealDishes属性
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //遍历集合得到每一个对象
        for (SetmealDish setmealDish : setmealDishes) {
            //设置关联字段
            setmealDish.setSetmealId(setmealDto.getId());
           //Long id = dishService.lambdaQuery().eq(Dish::getName, setmealDish.getName()).one().getId();
           // setmealDish.setDishId(id);
        }
        //调用setmealDishService层添加集合方法添加信息
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 自定义根据id查询套餐方法
     */
    @Override
    public SetmealDto selectById(Long id) {
        return  setmealMapper.selectSetmealDtoById(id);
    }

    /**
     * 修改套餐
     * @param setmealDto
     */
    @Override
    public void updateSetmealDto(SetmealDto setmealDto) {
        //调用本类修改方法
        this.updateById(setmealDto);
        //获取SetmealDish表信息
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //调用setmealDishService层方法,
        setmealDishService.lambdaUpdate()
                //判断是否相等
                .eq(SetmealDish::getSetmealId,setmealDto.getId())
                //删除相等的信息
                .remove();
        //遍历出每一个对象
        for (SetmealDish setmealDish : setmealDishes) {
            //设置关联字段
           setmealDish.setSetmealId(setmealDto.getId());
        }
        //调用setmealDishService层添加方法
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    public void deleteByIds(Long[] ids) {
        Integer count = this.lambdaQuery()
                .in(Setmeal::getId, ids)
                .eq(Setmeal::getStatus,1)
                .count();
        if (count>0){
             throw new CustomException("套餐在售中,不可删除");
        }
        this.removeByIds(Arrays.asList(ids));
        for (Long id : ids) {
            setmealDishService.lambdaUpdate().eq(SetmealDish::getSetmealId,id).remove();
        }
    }

    @Override
    public void updateStatus(Integer status, Long[] ids) {
        this.lambdaUpdate()
                .in(Setmeal::getId,ids)
                .set(Setmeal::getStatus,status)
                .update();
    }
}
